local midang = fk.CreateSkill{
  name = "plum__midang",
}

Fk:loadTranslationTable{
  ["plum__midang"] = "密党",
  [":plum__midang"] = "你未抵消对你生效的即时牌后，你可以选择一名角色，下张即时牌对其生效前，抵消方式改为令你摸一张牌。摸牌阶段，你可以改为与这些角色各摸一张牌。",

  ["#plum__midang-choose"] = "密党：可以选择一名角色，下张即时牌对其生效前，抵消方式改为令你摸一张牌",
  ["#plum__midang-draw"] = "密党：可以改为与 %arg 各摸一张牌",
  ["@@plum__midang"] = "密党",

  ["$plum__midang1"] = "",
  ["$plum__midang2"] = "",
}

midang:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("plum__midang_response")
end)

midang:addEffect(fk.CardEffectFinished, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(midang.name) and data.to and data.to == player
    and (data.card.type == Card.TypeBasic or data.card:isCommonTrick())
    and data.card.extra_data and data.card.extra_data.midang_quit
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = room:getAlivePlayers(false),
      max_num = 1,
      min_num = 1,
      skill_name = midang.name,
      cancelable = true,
      prompt = "#plum__midang-choose"
    })
    if #to == 1 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self) ---@type ServerPlayer
    if to:isAlive() then
      room:setPlayerMark(to, "@@plum__midang", 1)
      room:setPlayerMark(to, "plum__midang_boss", player.id)
    end
  end
})

local function isMidang(player)
  return player:getMark("@@plum__midang") == 1
end

midang:addEffect(fk.EventPhaseProceeding, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(midang.name) and player == target
    and data.phase == Player.Draw and not data.phase_end
    and table.find(player.room:getAlivePlayers(false), isMidang)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = table.filter(room:getAlivePlayers(false), isMidang)
    local tail = table.concat(table.map(tos, function (p)
      return Fk:translate(p.general)
    end), "，")
    return room:askToSkillInvoke(player, {
      skill_name = midang.name, prompt = "#plum__midang-draw:::"..tail
    })
  end,
  on_use = function (self, event, target, player, data)
    data.phase_end = true
    local tos = {player}
    table.forEach(player.room:getAlivePlayers(true), function (p)
      if isMidang(p) then table.insert(tos, p) end
    end)
    table.forEach(tos, function (p)
      p:drawCards(1, midang.name)
    end)
  end
})

midang:addEffect(fk.AfterAskForNullification, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(midang.name)
    and data.eventData and data.eventData.to == player
    and not data.eventData.isCancellOut
  end,
  on_refresh = function (self, event, target, player, data)
    local extra_data = data.eventData.card.extra_data or {}
    extra_data.midang_quit = not data.result
    data.eventData.card.extra_data = extra_data
  end
})

midang:addEffect(fk.AfterAskForCardUse, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(midang.name) and player == target
    and data.eventData and data.eventData.to == player
    and not data.eventData.isCancellOut
  end,
  on_refresh = function (self, event, target, player, data)
    local extra_data = data.eventData.card.extra_data or {}
    extra_data.midang_quit = not data.result
    data.eventData.card.extra_data = extra_data
  end
})

return midang